import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;

/**
 * 
 * List containing Commodity objects, along with method to read them in from a file.
 *
 */
public class CommodityList extends BaphiList {
	
	/**
	 * List containing Commodity objects
	 * @param fileName - file to be opened for reading
	 * @throws FileNotFoundException
	 * @throws NoSuchElementException
	 */
	public CommodityList(String fileName) throws FileNotFoundException, NoSuchElementException
	{
		readCommoditiesFromFile(fileName);
	}
	
	/**
	 * Returns Commodity at the provided index number
	 * @param idx
	 * @return Commodity
	 */
	public Commodity get(int idx)
	{
		return (Commodity) super.get(idx);
	}
	
	/**
	 * Opens the Commodities file and populates the list
	 * @param commFile - string containing the file path
	 * @throws FileNotFoundException
	 * @throws NoSuchElementException
	 */
	private void readCommoditiesFromFile(String fileName) throws FileNotFoundException, NoSuchElementException
	{
		File file = new File(fileName);
		Scanner scan;
		String cName;
		boolean isSticky;
		
		scan = new Scanner(file);
		scan.useDelimiter(",|\n");
		
		while(scan.hasNext())
		{	
			cName = scan.next();
			
			String stick = scan.nextLine();
			if (stick.startsWith(",Sticky"))
				isSticky = true;
			else
				isSticky = false;
			
			this.add(new Commodity(cName, isSticky));			
		}

		scan.close();
		
		return;
	}
}
